import Vue from "vue";
import "./plugins/axios";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import SuiVue from "semantic-ui-vue";
import "semantic/dist/semantic.min.css";
import "./registerServiceWorker";
import NProgress from "vue-nprogress";
import { sync } from "vuex-router-sync";
import * as filters from "./filters";
import { TOGGLE_SIDEBAR } from "./store/mutation-types";
import "./registerServiceWorker";
import Croppa from "vue-croppa";
import PortalVue from "portal-vue";
import Snotify, { SnotifyPosition } from "vue-snotify";
import { configureFakeBackend } from "./_helpers";
import VCalendar from "v-calendar";
import "v-calendar/lib/v-calendar.min.css";
import VueAMap from "vue-amap";
configureFakeBackend();
// 添加调试功能
process.env.NODE_ENV == "development" ? (Vue.config.devtools = true) : "";
Vue.router = router;
const snotifyOptions = {
  toast: {
    position: SnotifyPosition.centerTop
  }
};

Vue.use(VueAMap);
VueAMap.initAMapApiLoader({
  key: "62f8d08a3910e0c3d323e964192b12c3",
  plugin: ["AMap.Scale", "AMap.OverView", "AMap.ToolBar", "AMap.MapType"],
  v: "1.4.4"
});

// Use v-calendar, v-date-picker & v-popover components
Vue.use(VCalendar, {
  firstDayOfWeek: 2 // ...other defaults
});
Vue.use(Snotify, snotifyOptions);
Vue.use(SuiVue);
Vue.use(Croppa);
Vue.use(PortalVue);
const options = {
  latencyThreshold: 200, // Number of ms before progressbar starts showing, default: 100,
  router: true, // Show progressbar when navigating routes, default: true
  http: false // Show progressbar when doing Vue.http, default: true
};
Vue.use(NProgress, options);
// Effortlessly keep vue-router and vuex store in sync.
sync(store, router);
const nprogress = new NProgress({ parent: ".nprogress-container" });
// 从vuex中获取状态树
const { state } = store;
// 判断是否移动端或者是左侧栏是否显示
router.beforeEach((to, form, next) => {
  if (state.app.device.isMobile && state.app.sidebar.opened) {
    store.commit(TOGGLE_SIDEBAR, false);
  }
  // redirect to login page if not logged in and trying to access a restricted page
  const publicPages = ["/login"];
  const authRequired = !publicPages.includes(to.path);
  const loggedIn = localStorage.getItem("auth-token");
  if (authRequired && !loggedIn) {
    window.location.assign(`http://localhost:8080/sign/#/`);
    return next("/");
  } else {
    if (!state.authentication.status.loggedIn) {
      console.log("执行");
      const { dispatch } = store;
      dispatch("authentication/current");
    }
  }

  next();
});
// 获取filter所有的属性,添加到vue.filter
// vue.filter : Vue.js 允许你自定义过滤器，可被用于一些常见的文本格式化
Object.keys(filters).forEach(key => {
  Vue.filter(key, filters[key]);
});

new Vue({
  router,
  store,
  nprogress,
  render: h => h(App)
}).$mount("#app");
